package com.tencent.sr.iris.activity.dependency.user;

import com.alibaba.fastjson.JSON;
import com.tencent.sr.iris.user.core.service.interfaces.client.label.IrisUserLabelClient;
import com.tencent.sr.iris.user.core.service.interfaces.request.user.IrisQueryUserTagsRequest;
import com.tencent.sr.iris.user.core.service.interfaces.response.user.IrisQueryUsersInTagsResponse;
import com.tencent.sr.rmall.common.exception.retail.TRetailErrorException;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;

@Slf4j
@Component
public class UserLabelDependency {

    @Value("${iris.saasId}")
    private Long saasId;

    @Resource
    private IrisUserLabelClient irisUserLabelClient;


    public IrisQueryUsersInTagsResponse queryUserTags(String uid) {
        IrisQueryUserTagsRequest request = new IrisQueryUserTagsRequest();
        request.setSaasId(saasId);
        request.setUid(uid);
        log.info("查询用户标签信息，请求参数：{}", JSON.toJSONString(request));
        HttpResult<IrisQueryUsersInTagsResponse> result = irisUserLabelClient.queryUserTags(request);
        log.info("查询用户标签信息成功，请求参数：{}，返回结果：{}", JSON.toJSONString(request), JSON.toJSONString(result));
        if (result == null || !result.isSuccess()) {
            throw new TRetailErrorException("查询用户标签信息失败");
        }

        return Optional.ofNullable(result.getData())
                .orElseGet(IrisQueryUsersInTagsResponse::new);
    }

}
